package com.ligongmusic.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.ligongmusic.R;
import com.ligongmusic.domain.Upload;
import com.ligongmusic.util.GlobalParams;
import com.ligongmusic.util.LoggerUtils;
import com.ligongmusic.util.NetWorkUtils;
import com.ligongmusic.util.StringUtils;
import com.ligongmusic.util.UniversalAdapter;
import com.ligongmusic.util.ViewHolder;
import com.ligongmusic.view.FlowAdapter;
import com.ligongmusic.view.FlowLayout;
import com.ligongmusic.view.RoundImageView;
import com.ligongmusic.view.TopManager;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/30.
 */
public class PriseList extends Activity {
    private Upload musicInfo;
    private List<String> photos;
    private FlowLayout flowLayout;
    //    private GridView mGridView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.priselist);
        musicInfo = (Upload) getIntent().getSerializableExtra("comments");
        initView();
        getPrise();
    }

    private void initView() {
        TopManager.getTopManager(this).showTop(true, "赞", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        ImageView head = (ImageView) findViewById(R.id.user_head);
        ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + GlobalParams.userImageUrl, head);
        SpannableStringBuilder builder = new SpannableStringBuilder(GlobalParams.nickName + "分享了单曲");
        AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(30);
        ForegroundColorSpan span = new ForegroundColorSpan(getResources().getColor(R.color.color_red));
        builder.setSpan(span, 0, GlobalParams.nickName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(sizeSpan, 0, GlobalParams.nickName.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        ((TextView) findViewById(R.id.tv_share_info)).setText(builder);
        ((TextView) findViewById(R.id.tv_upload_date)).setText(/*musicInfo.getUpload_date() == null ? "" :*/ StringUtils.getYear(musicInfo.getUploaddate()));
        ImageView musicIcon = (ImageView) findViewById(R.id.music_icon);
        ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + musicInfo.getCover(), musicIcon);
        ((TextView) findViewById(R.id.tv_music_name)).setText(musicInfo.getSongname());
        ((TextView) findViewById(R.id.tv_singer)).setText(GlobalParams.nickName + "");
//        mGridView = (GridView) findViewById(R.id.grid);
        flowLayout = (FlowLayout) findViewById(R.id.flow);
    }

    public void getPrise() {
        List<NameValuePair> pairs = new ArrayList<NameValuePair>();
        pairs.add(new BasicNameValuePair("id", musicInfo.getId() + ""));
        NetWorkUtils.getRequest(pairs, GlobalParams.GET_PRISE_LIST, new NetWorkUtils.NetWorkCallBack() {
            @Override
            public void success(String result) {
                LoggerUtils.getLog(PriseList.class).error(result);
                try {
                    JSONArray arr = new JSONArray(result);
                    if (arr == null)
                        return;
                    photos = new ArrayList<String>();
                    for (int i = 0; i < arr.length(); i++) {
                        JSONObject obj = arr.optJSONObject(i);/*.optString("photo")*/
                        String nickname = obj.optString("musicianname");
                        photos.add(StringUtils.isEmpty(nickname) ? obj.optString("loginname") : nickname);
                    }
                    flowLayout.setAdapter(new FlowAdapter<String>(photos) {
                        @Override
                        public View getView(FlowLayout parent, int position, String s) {
//                            TextView tv = new TextView(PriseList.this);
//                            tv.setPadding(R.dimen.x10, R.dimen.x10, R.dimen.x10, R.dimen.x10);
//                            tv.setText(s);
                            View view = View.inflate(PriseList.this, R.layout.grid_item, null);
                            TextView tv = (TextView) view.findViewById(R.id.tv_item);
                            if (position != photos.size())
                                tv.setText(s + "、");
                            else
                                tv.setText(s);
                            return view;
                        }
                    });
//                    mGridView.setAdapter(new UniversalAdapter<String>(PriseList.this, photos, R.layout.grid_item) {
//                        @Override
//                        public void convert(ViewHolder holder, String s) {
//                            RoundImageView image = holder.getView(R.id.iv_photo);
//                            ImageLoader.getInstance().displayImage(GlobalParams.IMAGE_URL + s, image);
//                        }
//                    });
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void error() {
                LoggerUtils.getLog(PriseList.class).error("error");
            }

            @Override
            public void error(String message) {
                LoggerUtils.getLog(PriseList.class).error(message);

            }
        });
    }
}
